// ActionScript file
package com.app.business{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.app.factories.DosenFactory;
	import com.app.model.ModelLocator;
	import com.app.vo.DosenVO;
	import com.app.vo.MahasiswaVO;
	import com.app.vo.VO;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
    public class DosenDelegate implements IResponder{
        protected var responder:IResponder;
        protected var service:HTTPService;
        protected var dosenVO:DosenVO;
        protected var mahasiswaVo:MahasiswaVO;

        protected var loginVo:VO;
        protected var Act:String;
        protected var Com:String;
        protected var Param:String;
        protected var rs:Array;
        
	    public function DosenDelegate (responder : IResponder){
	        this.service = ServiceLocator.getInstance().getHTTPService( "services" );
	        this.responder = responder;
	        this.rs=new Array();
	    }
	    
	    public function getInfoDosen(dosenVo:DosenVO):void{
	    	if(dosenVo.dosenId!=null)
	    	  this.Param= dosenVo.dosenId;
	    	else
	          this.Param= dosenVo.dosenUsername;
	        this.Act='getInfoDosen';
	        this.Com='dosen';
	        var token:AsyncToken=service.send({com:Com, act:Act,params:Param});
	        token.addResponder(this);
	        this.dosenVO=dosenVo;
        }
        
        public function getListMahasiswa(dosenVo:DosenVO):void{
           this.Param = dosenVo.dosenId;
           this.Act = 'getListMahasiswa';
           this.Com = 'dosen';
           var token:AsyncToken=service.send({com:Com,act:Act,params:Param});
           token.addResponder(this);
           this.dosenVO=dosenVo;
        }
        
        public function doUpdateProfilDosen(dosenVo:DosenVO):void{
           this.Param = dosenVo.dosenNama + '|' + dosenVo.dosenAlamatRumah + '|' + dosenVo.dosenAlamatkantor + '|' + dosenVo.dosenEmail + '|' + dosenVo.dosenNoTelp + '|' + dosenVo.dosenId;
          // Alert.show(this.Param);
           this.Act ="doUpdateDosen";
           this.Com = "dosen"; 
           var token:AsyncToken=service.send({com:Com,act:Act,params:Param});
           token.addResponder(this);
           this.dosenVO=dosenVo;
        }
        
	    public function result(data:Object):void{
	        var event:ResultEvent= ResultEvent(data);
	        //Alert.show(event.result as String); //uncommand this line to debug result of services
	        if(event.result!='false'){
	        	if(this.Act=='getInfoDosen'){
                    this.dosenVO=DosenFactory.buildInfoDosen(event.result as String,this.dosenVO);
		            responder.result(this.dosenVO);
	            }else if(this.Act=='getListMahasiswa'){
	               this.dosenVO=DosenFactory.buildListMahasiswa(event.result as String,this.dosenVO);
	               responder.result(this.dosenVO);
	            }else if(this.Act=='doUpdateDosen'){
	               responder.result(this.dosenVO);
	            }
	        }else{
	        	responder.fault(null);
	        }
	    }
	    
	    public function fault(info:Object):void{
	        var err:FaultEvent=FaultEvent(info);
	        ModelLocator.getInstance().dosen.statusMessage='Get Info Dosen Failed';
	    }
    
    }
}